package dk.hewison.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 16:53:28 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 348 $:
 */
class FolderInfo {
    boolean containsImages;
    String name;

    FolderInfo parent;
    List<FolderInfo> folderInfos = new ArrayList<FolderInfo>();
    List<String> imageFilenames = new ArrayList<String>();
    private File directoryFile;

    public FolderInfo(FolderInfo parentFolderInfo, File directoryFile) {
        this.parent = parentFolderInfo;
        this.directoryFile = directoryFile;
        name = directoryFile.getName();
    }

    void addImageFilename(String imageFilename) {
        imageFilenames.add(imageFilename);
        setContainsImages();
    }

    private void setContainsImages() {
        if (containsImages) return;

        if (parent != null) {
            parent.setContainsImages();
        }
        containsImages = true;
    }

    public File getDirectoryFile() {
        return directoryFile;
    }

    public List<String> getImageFilenames() {
        return imageFilenames;
    }

    void addFolderInfo(FolderInfo folderInfo) {
        folderInfos.add(folderInfo);
    }

    public List<FolderInfo> getFolderInfos() {
        return folderInfos;
    }

    /**
     * {
     * "name" : "John",
     * "albums" : [
     * {
     * "folderName": "1", "name": "hello", "caption": "Well",  "description": "", "coverPhoto": "P1000379.jpg"
     * },{
     * "folderName": "2", "name": "jul","caption": "What","description": "","coverPhoto": "P1000406.jpg"
     * }
     * ]}
     * <p/>
     * {"name": "2009","albums":[
     * {"folderName": "01:01:2009","hasImages": "true","coverPhoto": "MEMO0010.JPG"},
     * {"folderName": "04:01:2009","hasImages": "true","coverPhoto": "P1000592.JPG"}]}
     *
     * @return
     */
    String getSubFoldersString() {
        StringBuffer buf = new StringBuffer("{");
        appendJSONValue(buf, "name", name);
        buf.append(",\"subdir\":[");
        if (!folderInfos.isEmpty()) {
            for (FolderInfo folderInfo : folderInfos) {
                if (!folderInfo.containsImages) continue;
                buf.append("{");
                appendJSONValue(buf, "folderName", folderInfo.name);
                buf.append(",");
                appendJSONValue(buf, "hasImages", Boolean.toString(folderInfo.containsImages));
                buf.append(",");
                appendJSONValue(buf, "coverPhoto", folderInfo.getCoverImageName());
                buf.append("},");
            }
            buf.replace(buf.length() - 1, buf.length(), "");
        }

        buf.append("]}");
        return buf.toString();
    }

    private void appendJSONValue(StringBuffer buf, String jsonName, String value) {
        String v = "\"" + jsonName + "\": \"" + value + "\"";
        buf.append(v);
    }

    String getImageNamesString() {
        StringBuffer buf = new StringBuffer();
        for (String imageFilename : imageFilenames) {
            buf.append(imageFilename).append("\n");
        }
        return buf.toString();
    }

    public String getCoverImageName() {
        if (imageFilenames.isEmpty()) return "";
        String cover = imageFilenames.get(0);
        if (cover == null) return "";
        return cover;
    }
}